/*
 *
 *  Copyright (C) 2009 GSyC/LibreSoft
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Roberto Calvo Palomino <rocapal@gsyc.es>
 *
 */

package com.LibreGeoSocial.App;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.LibreGeoSocial.Api.User;
import com.LibreGeoSocial.App.OMFActivity.SMS;
import com.LibreGeoSocial.App.Routes.LocationService;
import com.LibreGeoSocial.App.Utils.LocationUtils;


public class UserCard extends GenericCard {

	private ImageView imageUser;
	private TextView lbTitle, lbBody, lbDist;
	private User mUser;
	
	private static final int MENU_CALL = Menu.FIRST + 10;
	private static final int MENU_SMS = Menu.FIRST + 20;
	
	private static final int DIALOG_QUESTION_SMS = 1;
	
    public void onCreate(Bundle icicle) {
    	
         super.onCreate(icicle);
        
         setContentView(R.layout.generic_card);
       	 
         mUser = (User) mNode;
         
         imageUser = (ImageView)this.findViewById(R.id.img);
         lbTitle = (TextView) this.findViewById(R.id.lbTitle);
         lbBody = (TextView) this.findViewById(R.id.lbBody);   
         lbDist = (TextView) this.findViewById(R.id.lbDist);
                  
         lbTitle.setText (mUser.getName());
         lbBody.setText  (mUser.getState()); 
         
         // Calculate distance
         float dist = LocationUtils.calculateDistance (LocationService.getCurrentLocation(),
        		 	mUser.getLocation());
         
         lbDist.setText  ("Distance: " + LocationUtils.displayDistance(dist, LocationUtils.AUTO));

         if (mUser.getAvatarId()==null)
         	imageUser.setImageResource(R.drawable.user_48);
         else
         	imageUser.setImageBitmap(mUser.getAvatarBitmap());
         
         setIconId(R.drawable.user_48);
         showMap(mUser.getLongitude(), mUser.getLatitude());

        
    }

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
        super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_CALL, 0, "Call friend")
        		.setIcon(R.drawable.phone)
                .setAlphabeticShortcut('C');
        
        SharedPreferences sharedPreferences = 
	    	PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        
        if(sharedPreferences.getBoolean(LGS_Preferences.KEY_MOVISTAR_ACTIVE, false))
        	menu.add(0, MENU_SMS, 0, "Send SMS")
        		.setIcon(R.drawable.paper_pencil_48)
        		.setAlphabeticShortcut('S');
        
        return true;
    }

    @Override
    public boolean onOptionsItemSelected (MenuItem item) {
    	
    	switch (item.getItemId()) {
    	
    	case MENU_CALL:
    		SharedPreferences sharedPreferences = 
    	    	PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    		
    		if(lbBody.getText().toString().toUpperCase().equals("BUSY") && 
    				sharedPreferences.getBoolean(LGS_Preferences.KEY_MOVISTAR_ACTIVE, false)){
    			
    			showDialog(DIALOG_QUESTION_SMS);
    		}else{
    			Intent i = new Intent(Intent.ACTION_DIAL);
            	startActivity(i);
    		}
    		break;
    		
    	case MENU_SMS:
        	
    		Intent i = new Intent(this,SMS.class);
        	startActivity(i);
        	
    		break;
    	
        }
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    protected Dialog onCreateDialog(int id) { 
        
    	switch (id) {
	    	case DIALOG_QUESTION_SMS:
	    		
	    		LayoutInflater factory = LayoutInflater.from(this);
	    		final View textEntryView = factory.inflate(R.layout.sms_warning, null);
	    		
	    		
		        return new AlertDialog.Builder(this)	        
		        .setTitle("Warning!")
		        .setView(textEntryView)
		        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		            public void onClick(DialogInterface dialog, int whichButton) {
		            	//launch activity
		            	Intent i = new Intent(UserCard.this,SMS.class);
		            	startActivity(i);
		                /* User clicked OK so do some stuff */
		            }
		        })
		        .setNegativeButton("No", new DialogInterface.OnClickListener() {
		            public void onClick(DialogInterface dialog, int whichButton) {
		            	
		                /* User clicked cancel so do some stuff */
		            }
		        })
		        .create();
		    
    	}
    	
    	return null;
    }
}
